package com.iflytek.universitymanagement.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iflytek.universitymanagement.config.Result;
import com.iflytek.universitymanagement.domain.Competition;
import com.iflytek.universitymanagement.domain.CompetitionParticipationDTO;
import com.iflytek.universitymanagement.domain.Course;
import com.iflytek.universitymanagement.service.CompetitionService;
import com.iflytek.universitymanagement.service.CourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/competition")
public class CompetitionController {
    @Autowired
    CompetitionService competitionService;


        /**
         * 新增
         */
        @PostMapping("/add")
        public Result add(@RequestBody Competition competition) {
            competitionService.save(competition);
            return Result.success();
        }

        /**
         * 删除
         */
        @DeleteMapping("/delete/{id}")
        public Result deleteById(@PathVariable Integer id) {
            competitionService.removeById(id);
            return Result.success();
        }

        /**
         * 批量删除
         */
        @DeleteMapping("/delete/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
            competitionService.removeByIds(ids);
            return Result.success();
        }

        /**
         * 修改
         */
        @PutMapping("/update")
        public Result updateById(@RequestBody Competition competition) {
            competitionService.updateById(competition);
            return Result.success();
        }

        /**
         * 根据ID查询
         */
        @GetMapping("/selectById/{id}")
        public Result selectById(@PathVariable Integer id) {
            Competition competition = competitionService.getById(id);
            return Result.success(competition);
        }


        /**
         * 查询所有
         */
        @GetMapping("/selectAll")
        public Result selectAll( ) {
            List<Competition> list = competitionService.list();
            return Result.success(list);
        }



    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(required = false) String competitionName,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        QueryWrapper<Competition> queryWrapper = new QueryWrapper<Competition>().orderByAsc("competition_id");
        if (competitionName != null && !competitionName.trim().isEmpty()) {
            queryWrapper.like("name", competitionName);
        }
        Page<Competition> page = competitionService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }



    @GetMapping("/all-students")
    public Page<CompetitionParticipationDTO> getAllStudentCompetitionInfoWithPagination(
            @RequestParam(defaultValue = "1") int pageNum, // 当前页码，默认为1
            @RequestParam(defaultValue = "10") int pageSize // 每页大小，默认为10
    ) {
        return competitionService.getAllStudentCompetitionInfoWithPagination(pageNum, pageSize);
    }

}



